//
//  SkillChoiceViewController.m
//  RosettaApplication2
//
//  Created by Taylor White on 11/21/13.
//  Copyright (c) 2013 Miami University. All rights reserved.
//

#import "SkillChoiceViewController.h"

@interface SkillChoiceViewController ()

@end

@implementation SkillChoiceViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.navigationItem.hidesBackButton = YES;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    #define rows 3
    #define columns 3
    
    skills = 0;
    
    for(int i = 0; i < 3; i ++)
    {
        selected[i] = -1;
    
    }
    
    
    int i = 0;
    for(int y = 0; y < 3; y++)
    {
        for(int x = 0; x < 3;x++)
        {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            button.frame = CGRectMake(150 + 200 * x, 300 + 200 * y, 150, 150);
            unsigned buttonNumber = y * 3 + x;
            button.tag = buttonNumber;
            
            [button setHighlighted:false];
            [button setBackgroundColor:[UIColor grayColor]];
            
            
            
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            
            NSString *skillNum = [NSString stringWithFormat:@"%@%u", @"option", i];
            NSString *thisSkill = [defaults objectForKey:skillNum];
            
            NSLog(@"selected[0] %@ ", thisSkill);
            
            
            [button setTitle:[NSString stringWithFormat:@"%@", thisSkill] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview: button];
            i++;
        }
        
    }
    
    
    
    
}

- (void)buttonPressed:(UIButton *) button
{
    UIColor* rosettaPink = [UIColor colorWithRed:0xFF/255.0f
                                           green:0x00/255.0f
                                            blue:0x84/255.0f alpha:1];
    
    UIColor* rosettaBlue = [UIColor colorWithRed:0x00/255.0f
                                           green:0xCD/255.0f
                                            blue:0xFF/255.0f alpha:1];

    
    
    if(button.selected)
    {
        [button setTitleColor:rosettaBlue forState:UIControlStateNormal];
        [button setSelected:false];
        
        for(int i = 0; i < 3; i++)
        {
            if(selected[i] == button.tag)
            {
                if(i == 2)
                    selected[2] = -1;
                else if(i == 1)
                {
                    selected[1] = selected[2];
                    selected[2] = -1;
                }
                else if(i == 0)
                {
                    selected[0] = selected[1];
                    selected[1] = selected[2];
                    selected[2] = -1;
                }
            }
        }
        
        skills--;
    }
        
    else if(skills < 12)
    {
        if(skills == 0)
            selected[0] = button.tag;
        
        else if (skills == 4)
            selected[1] = button.tag;
        
        else if (skills == 8)
            selected[2] = button.tag;
        
        [button setTitleColor:rosettaPink forState:UIControlStateNormal];
        [button setSelected:true];
        skills++;
    }
    
    
    /*
    NSLog(@"selected[0] %u ", selected[0]);
    NSLog(@"selected[1] %u ", selected[1]);
    NSLog(@"selected[2] %u ", selected[2]);
    NSLog(@"button tag %u ", button.tag);
    NSLog(@"button %u -- frame: %@", skills, NSStringFromCGRect(button.frame));
    */
    
    
    NSString *skill0 = [NSString stringWithFormat:@"%@%u", @"option", selected[0]];
    NSString *skill1 = [NSString stringWithFormat:@"%@%u", @"option", selected[1]];
    NSString *skill2 = [NSString stringWithFormat:@"%@%u", @"option", selected[2]];
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    
    
    NSString *title0 = [defaults objectForKey:skill0];
    NSString *title1 = [defaults objectForKey:skill1];
    NSString *title2 = [defaults objectForKey:skill2];
    
    
    [defaults setObject:title0 forKey:@"skill0"];
    [defaults setObject:title1 forKey:@"skill1"];
    [defaults setObject:title2 forKey:@"skill2"];
    
    //NSLog(@"skill0 %@ ", title0);
    //NSLog(@"skill1 %@ ", title1);
    //NSLog(@"skill2 %@ ", title2);
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
